package cn.tedu.boot52.controller;

import cn.tedu.boot52.entity.Comment;
import cn.tedu.boot52.entity.User;
import cn.tedu.boot52.mapper.CommentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
public class CommentController {
    @Autowired(required = false)
    CommentMapper mapper;
    @RequestMapping("/comment")
    public int comment(Comment comment, HttpSession session){
        //得到当前登入的用户信息
        User user= (User) session.getAttribute("user");
        if (user==null){//未登入
            return 2;
        }
        comment.setCreated(new Date());
        comment.setAuthor(user.getNick());
        mapper.insert(comment);
        return 1;
    }
    @RequestMapping("/selectComment")
    public List<Comment>selectComment(int id){
        return mapper.selectByWid(id);
    }
}
